/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.cholm.media.castdr.loaders;

// import java.util.List;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.backend.Program;
import org.cholm.media.castdr.backend.Serie;

import java.util.ArrayList;
import java.util.List;

import static org.cholm.media.castdr.LogUtils.LOGd;
import static org.cholm.media.castdr.LogUtils.LOGw;

/**
 * Loader of programs
 */
public class ProgramLoader extends AsyncTaskLoader<List<Program>> {

    public static final String TAG = LogUtils.makeLogTag(ProgramLoader.class);
    private List<Program> mData = new ArrayList<Program>();
    private Serie mSerie = null;

    public ProgramLoader(Context context, Serie s) {
        super(context);
        mSerie = s;
        LOGd(TAG, "Created loader");
    }

    public void set(Serie serie) {
        mSerie = serie;
    }

    public Serie get() {
        return mSerie;
    }

    protected void add(Program s) {
        LOGd(TAG, "Adding serie " + s.getTitle());
        mData.add(s);
    }

    @Override
    public List<Program> loadInBackground() {
        if (mSerie == null) {
            LOGw(TAG, "No Serie object set");
            return null;
        }
        LOGd(TAG, "Loading in background from " + mSerie.getSlug());
        if (!mSerie.load()) {
            LOGw(TAG, "Failed to load the serie " + mSerie.getSlug());
            // mData = null;
            return null;
        }
        // else {
        // for (Program p : mSerie.getPrograms())
        // mData.add(p);
        // Collections.sort(mData, new Comparer());
        return mSerie.getPrograms();
        // }
        //
        // return null;
    }

    @Override
    protected void onStartLoading() {
        LOGd(TAG, "Starting load");
        super.onStartLoading();
        forceLoad();
    }

    @Override
    protected void onReset() {
        LOGd(TAG, "Clearing data");
        mSerie = null;
        // mData.clear();
    }

    @Override
    protected void onForceLoad() {
        LOGd(TAG, "Force loading");
        // mData.clear();
        super.onForceLoad();
    }

    /**
     * Handles a request to stop the Loader.
     */
    @Override
    protected void onStopLoading() {
        // Attempt to cancel the current load task if possible.
        LOGd(TAG, "Stopping load");
        cancelLoad();
    }
}
/* EOF */

